package demo4.controller;

import demo4.mapper.IUserMapper;
import demo4.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Scanner;

@Component
public class UserController {
    @Autowired
    private IUserMapper userMapper;
    public void start(){
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入编号：");
        System.out.println("1.用户注册");
        System.out.println("2.用户登录");
        System.out.println("3.用户列表");
        System.out.println("4.修改用户信息");
        int num = scanner.nextInt();
        switch(num){
            case 1:
                userReg();
                break;
            case 2:
                userLogin();
                break;
            case 3:
                userList();
                break;
            case 4:
                userUpdate();
                break;
            default:
                System.out.println("输入错误");
                break;
        }
    }

    private void userReg(){
        System.out.println("欢迎注册");
        User user = new User();
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入用户名：");
        user.setUsername(scanner.nextLine());
        System.out.println("请输入密码：");
        user.setPassword(scanner.nextLine());
        System.out.println("请输入昵称：");
        user.setNickname(scanner.nextLine());
        System.out.println("请输入年龄：");
        user.setAge(scanner.nextInt());
        System.out.println(user);
        if(userMapper.selectByUsername(user.getUsername()) == null){
            int result = userMapper.insert(user);
            System.out.println(result>0?"注册成功":"注册失败");
        }else{
            System.out.println("用户【"+user.getUsername()+"】已存在！");
        }

    }

    private void userLogin(){
        System.out.println("欢迎登录！");
        System.out.println("请输入用户名：");
        Scanner scanner = new Scanner(System.in);
        String username = scanner.nextLine();
        System.out.println("请输入密码：");
        String password = scanner.nextLine();
        User user = userMapper.selectByUsername(username);
        if(user != null){
            if(user.getPassword().equals(password)){
                System.out.println("登录成功！欢迎【"+user.getNickname()+"】回来！");
                System.out.println(user);
            }else{
                System.out.println("登录失败,密码错误！");
            }
        }else{
            System.out.println("用户不存在！");
        }
    }

    private void userList(){
        System.out.println("展示用户列表");
        List<User> users = userMapper.selectAll();
        for(User user:users){
            System.out.println(user);
        }
    }

    private void userUpdate(){
        System.out.println("修改用户信息...");
        System.out.println("请输入用户名：");
        User user = new User();
        Scanner scanner = new Scanner(System.in);
        user.setUsername(scanner.nextLine());
        System.out.println("请输入新的密码：");
        user.setPassword(scanner.nextLine());
        System.out.println("请输入新的昵称：");
        user.setNickname(scanner.nextLine());
        System.out.println("请输入新的年龄：");
        user.setAge(scanner.nextInt());
            int num = userMapper.update(user);
            System.out.println(num>0?"修改成功":"修改失败，用户【"+user.getUsername()+"】不存在！");
    }
}
